package com.kay.springboot_es_api.utils;

import org.frameworkset.elasticsearch.entity.InnerSearchHit;
import org.frameworkset.elasticsearch.entity.InnerSearchHits;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Kay on 2018/11/4
 */
public class MyResultUtil {
    public static List getInnerHits(Map<String, Map<String, InnerSearchHits>> innerHits, String indexType) {
        if (innerHits != null && innerHits.size() != 0) {
            Map<String, InnerSearchHits> hits = (Map)innerHits.get(indexType);
            if (hits != null) {
                InnerSearchHits ihits = (InnerSearchHits)hits.get("hits");
                if (ihits != null) {
                    List<InnerSearchHit> temp = ihits.getHits();
                    if (temp.size() == 0) {
                        return null;
                    }

                    List ts = new ArrayList(temp.size());

                    for(int i = 0; i < temp.size(); ++i) {
                        ts.add(((InnerSearchHit)temp.get(i)).getSource());
                    }

                    return ts;
                }
            }

            return null;
        } else {
            return null;
        }
    }
}
